package com.samsung.spensdk.example.simple;

/*
 * Sample application constraint
 * Max VM Application heap size should not be less than 64 Mbyte
 */
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import android.R.string;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.IntToString;
import android.widget.Button;
import android.widget.RelativeLayout;



import com.samsung.samm.common.SObjectStroke;
import com.samsung.sdraw.PenSettingInfo;
import com.samsung.spen.lib.input.SPenEvent;
import com.samsung.spen.settings.SettingStrokeInfo;
import com.samsung.spensdk.SCanvasConstants;
import com.samsung.spensdk.SCanvasView;
import com.samsung.spensdk.applistener.HistoryUpdateListener;
import com.samsung.spensdk.applistener.SCanvasInitializeListener;
import com.samsung.spensdk.applistener.SPenHoverListener;
import com.samsung.spensdk.applistener.SPenTouchListener;
import com.samsung.spensdk.applistener.SettingStrokeChangeListener;

public class SPen_Example_Simple extends Activity {

	public static final String TAG = "Example1";

	public static final String DEFAULT_APP_IMAGEDATA_DIRECTORY = "SPenSDK2.1/images";
    
	public static  DatagramSocket UDPsocket;
	public static InetAddress serverAddr;
	
	public static final String EXTRA_IMAGE_PATH = "path";
	public static final String EXTRA_IMAGE_NAME = "filename";

	public static final String SAVED_FILE_EXTENSION = "png";

	private static final int DIALOG_CHANGE_BACKGROUND = 1;
	public connectThread cThread = null;
	
	private SCanvasView mSCanvas;
	private Button mPenBtn;
	private Button mEraserBtn;
	private Button mUndoBtn;
	private Button mRedoBtn;
	private File mFolder = null;
	Context mContext = null;
	SPenEvent mSepn;
	private int mCheckedItem;
	private float myStrokeWidth = 1.0f;
	private int connected = 0;
	int vvv =0;
	float GpreX;
	float GpreY;
	float GX;
	float GY;
	int flag  = 0;
	private int i = 0;
	final PenSettingInfo PP = new PenSettingInfo();
	public Socket cSocke = null;
	 PrintWriter aa =null;
	public PrintWriter streamOut = null;
	 public BufferedReader streamIn = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		
		
		setContentView(R.layout.simple_main);

		mContext = this;

		mCheckedItem = 0;
		mPenBtn = (Button) findViewById(R.id.settingBtn);
		mPenBtn.setOnClickListener(mBtnClickListener);
		mPenBtn.setTextColor(Color.WHITE);

		mEraserBtn = (Button) findViewById(R.id.eraseBtn);
		mEraserBtn.setOnClickListener(mBtnClickListener);
		mUndoBtn = (Button) findViewById(R.id.undoBtn);
		mUndoBtn.setOnClickListener(undoBtnClickListener);
		mRedoBtn = (Button) findViewById(R.id.redoBtn);
		mRedoBtn.setOnClickListener(redoBtnClickListener);

		mSCanvas = (SCanvasView) findViewById(R.id.canvas_view);
	/*	
		  try {
			   cSocket = new Socket("210.118.69.110", 7654);
			 //  streamOut = new PrintWriter(cSocket.getOutputStream(), true);      // 출력용 스트림
			 //  streamIn = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));  // 입력용 스트림
			   
			//   sendMessage("# 새로운 이용자[" + user + "]님이 들어왔습니다.");
			   
			 //  cThread = new chatThread();
			 //  cThread.start();
			  }catch (UnknownHostException ue) {
				  Log.d("device","서버에러");
				 
			  }catch(IOException ie){
				  Log.d("device","소켓에러");
			  }
		*/


	
		
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
		
		
		mSCanvas.setSPenTouchListener(new SPenTouchListener() {

			public boolean onTouchPenEraser(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				return false;
			}

			@SuppressLint("NewApi")
			public boolean onTouchPen(View view, MotionEvent event) {
				// TODO Auto-generated method stub
				if(flag == 0){
				float x;
				float y;

				i++;

				
		
		    	
			
				x = event.getX();
				y = event.getY();
				String.format("%.1f", x);
				String.format("%.1f", y);
				//Log.d("TakeNote", "a : " + a);
				 UDPsendMessage(x, y);
				 Log.d("TakeNote", "x:" + x + ",,,y:" + y);
				}
				  // sendMessage(String.format("%.1f", x));
				//  sendMessage(String.format("%.1f", y));
		
				 
				 // Log.d("deviceee", "x:" + x + ",,,y:" + y);
				return false;
			}
			
			public boolean onTouchFinger(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				
				// PointF[] ff;
				// SettingStrokeInfo strokeInfo;
				// StrokeInfo ss = null;
				// strokeInfo = mSCanvas.getSettingStrokeInfo ();
				// ff = ss.points;
				// System.out.println(ff);
				// mSCanvas.setPenSettingInfo(PP);
				// strokeInfo.setStrokeWidth(myStrokeWidth);
				// mSCanvas.setSettingStrokeInfo (strokeInfo);
				return false;
			}

			public void onTouchButtonUp(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				float GR = 0;
				GX = event.getX();
				GY = event.getY();
				Log.d("SideButtonUp", "x:" + GX + ",,,y:" + GY);
				GR = GpreX - GX;
				if(GR > 10){
					char x = '#';
					char y = '3';
				
					//Log.d("TakeNote", "a : " + a);
					 UDPsendMessage(x, y);
				flag = 0;
						Log.d("deviceee", "X이벤트 성공 :" + GR);
				}else if(GR < -10){
					char x = '#';
					char y = '2';
				
					//Log.d("TakeNote", "a : " + a);
					 UDPsendMessage(x, y);
					 flag = 0;
					Log.d("deviceee", "Y이벤트성공 :" + GR);
				}
			
				// mSCanvas.showSettingView(AbstractSettingView.PEN_SETTING_VIEW);   
				Log.d("deviceee", "SUp event 감지");
			}

			public void onTouchButtonDown(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				//				PP.setPenType(PP.getPenType());
				//PP.setPenColor(Color.BLUE);
				// PP.setPenAlpha(5);
				//	mSCanvas.setPenSettingInfo(PP);
				flag = 1;
				GpreX = event.getX();
				GpreY = event.getY();
				Log.d("deviceee", "x:" + GpreX + ",,,y:" + GpreY);
				//mSCanvas.toggleShowSettingView(SCanvasConstants.SCANVAS_SETTINGVIEW_PEN);
				
				Log.d("deviceee", "event 감지");
			}
		});
		mSCanvas.setSPenHoverListener(new SPenHoverListener() {
			
			public void onHoverButtonUp(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				
			}
			
			public void onHoverButtonDown(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				char x = '#';
				char y = '9';
			
				//Log.d("TakeNote", "a : " + a);
				 UDPsendMessage(x, y);
			}
			
			public boolean onHover(View view, MotionEvent event) {
				// TODO Auto-generated method stub
				char x = '#';
				char y = '1';
			
				//Log.d("TakeNote", "a : " + a);
				 UDPsendMessage(x, y);
				return false;
			}
		});
			
		
		
		
		
		
		
		// Resource
		HashMap<String, Integer> settingResourceMap = new HashMap<String, Integer>();
		// Layout
		settingResourceMap.put(SCanvasConstants.LAYOUT_PEN_SPINNER,
				R.layout.mspinner);
		// Locale
		settingResourceMap.put(SCanvasConstants.LOCALE_PEN_SETTING_TITLE,
				R.string.pen_settings);
		settingResourceMap.put(SCanvasConstants.LOCALE_ERASER_SETTING_TITLE,
				R.string.eraser_settings);
		settingResourceMap.put(SCanvasConstants.LOCALE_ERASER_SETTING_CLEARALL,
				R.string.clear_all);
		settingResourceMap.put(
				SCanvasConstants.LOCALE_ERASER_SETTING_CLEARALL_MESSAGE,
				R.string.confirm_clear_all);

		// Create Setting View
		boolean bClearAllVisibileInEraserSetting = true;
		RelativeLayout settingViewContainer = (RelativeLayout) findViewById(R.id.canvas_container);
		mSCanvas.createSettingView(settingViewContainer, settingResourceMap,
				bClearAllVisibileInEraserSetting);

		mSCanvas.setSCanvasInitializeListener(mSCanvasInitializeListener);
		mSCanvas.setHistoryUpdateListener(mHistoryUpdateListener);
		mSCanvas.setSettingStrokeChangeListener(mSettingStrokeChangeListener);

		// create basic save/road file path
		File sdcard_path = Environment.getExternalStorageDirectory();
		mFolder = new File(sdcard_path, DEFAULT_APP_IMAGEDATA_DIRECTORY);
		if (!mFolder.exists()) {
			if (mFolder.mkdirs() == false) {
				Log.e(TAG, "Default Save Path Creation Error");
				return;
			}
		}

		// mUndoBtn.setEnabled(false);
		// mRedoBtn.setEnabled(false);

		cThread = new connectThread();
		   cThread.start();

	}
	
	

	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			if (bundle == null)
				return;
			String mFileName = bundle.getString(EXTRA_IMAGE_NAME);

			if (loadCanvas(mFileName))
				Log.d(TAG, "LOAD succeed");
			else
				Log.d(TAG, "LOAD failed");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.example_simple_menu, menu);
		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		boolean bSaveOnlyForeground = true;
		switch (item.getItemId()) {

		case R.id.options_save_fg_bg:
			Log.i(TAG, "SAVE Foreground with Background Image");
			bSaveOnlyForeground = false;
			if (saveCanvasImage(bSaveOnlyForeground))
				Log.d(TAG, "SAVE succeed");
			else
				Log.d(TAG, "SAVE failed");
			return true;
		case R.id.options_load_as_fg:
			Log.i(TAG, "LOAD");
			startFileListActivity();
			return true;

		}

		return false;
	}

	public boolean saveCanvasImage(boolean bSaveOnlyForegroundImage) {
		Bitmap bmCanvas = mSCanvas.getCanvasBitmap(bSaveOnlyForegroundImage);

		if (!(mFolder.exists()))
			if (!mFolder.mkdirs())
				return false;
		String savePath = mFolder.getPath()
				+ '/'
				+ ExampleUtils.getUniqueFilename(mFolder, "image",
						SAVED_FILE_EXTENSION);
		Log.d(TAG, "Save Path = " + savePath);

		return saveBitmapPNG(savePath, bmCanvas);
	}

	boolean saveBitmapPNG(String strFileName, Bitmap bitmap) {
		if (strFileName == null || bitmap == null)
			return false;

		boolean bSuccess1 = false;
		boolean bSuccess2;
		boolean bSuccess3;
		File saveFile = new File(strFileName);

		if (saveFile.exists()) {
			if (!saveFile.delete())
				return false;
		}

		try {
			bSuccess1 = saveFile.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		OutputStream out = null;
		try {
			out = new FileOutputStream(saveFile);
			bSuccess2 = bitmap.compress(CompressFormat.PNG, 100, out);
		} catch (Exception e) {
			e.printStackTrace();
			bSuccess2 = false;
		}
		try {
			if (out != null) {
				out.flush();
				out.close();
				bSuccess3 = true;
			} else
				bSuccess3 = false;

		} catch (IOException e) {
			e.printStackTrace();
			bSuccess3 = false;
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return (bSuccess1 && bSuccess2 && bSuccess3);
	}

	public boolean loadCanvas(String fileName) {

		String loadPath = mFolder.getPath() + '/' + fileName;

		Log.i(TAG, "Load Path = " + loadPath);

		Bitmap bmForeground = BitmapFactory.decodeFile(loadPath);
		int nWidth = mSCanvas.getWidth();
		int nHeight = mSCanvas.getHeight();
		bmForeground = Bitmap.createScaledBitmap(bmForeground, nWidth, nHeight,
				true);
		mSCanvas.setClearImageBitmap(bmForeground);

		Bitmap bm = null;	
		switch (mCheckedItem) {
		case 0:
			// bm = BitmapFactory.decodeResource(mContext.getResources(),
			// R.drawable.letter_bg_grass);
			break;
		case 1:
			// bm = BitmapFactory.decodeResource(mContext.getResources(),
			// R.drawable.letter_bg_present);
			break;
		case 2:
			// bm = BitmapFactory.decodeResource(mContext.getResources(),
			// R.drawable.letter_bg);
			break;
		case 3:
			// initBackground();
			break;
		}
		if (bm != null) {

		}

		return true;
	}

	// private String[] mBgNames = { "background 1", "background 2",
	// "background 3", "clear background", };

	// protected Dialog onCreateDialog(int id) {
	// switch (id) {
	// case DIALOG_CHANGE_BACKGROUND:
	// AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
	// builder.setSingleChoiceItems(mBgNames, mCheckedItem, new
	// DialogInterface.OnClickListener() {
	//
	// public void onClick(DialogInterface dialog, int which) {
	// mCheckedItem = which;
	// Bitmap bm = null;
	// switch(which) {
	// case 0:
	// // bm = BitmapFactory.decodeResource(mContext.getResources(),
	// R.drawable.letter_bg_grass);
	// break;
	// case 1:
	// // bm = BitmapFactory.decodeResource(mContext.getResources(),
	// R.drawable.letter_bg_present);
	// break;
	// case 2:
	// //bm = BitmapFactory.decodeResource(mContext.getResources(),
	// R.drawable.letter_bg);
	// break;
	// case 3:
	// //initBackground();
	// break;
	// }
	// if(bm != null) {
	// // mSCanvas.setBGImage(bm);
	// }
	// removeDialog(DIALOG_CHANGE_BACKGROUND);
	// }
	// });
	// return builder.create();
	// }
	// return super.onCreateDialog(id);
	// }

	// private void initBackground() {
	// //mSCanvas.setBGColor(0x00ffffff);
	// }

	public void startFileListActivity() {
		Intent intent = new Intent(getApplicationContext(), ListActivity.class);
		startActivityForResult(intent, 0);
	}

	private OnClickListener undoBtnClickListener = new OnClickListener() {
		public void onClick(View v) {
			if (v.equals(mUndoBtn)) {
				// mSCanvas.undo();

				final String items[] = { "Black", "Red", "Blue" };
				final SettingStrokeInfo strokeInfo;
				strokeInfo = mSCanvas.getSettingStrokeInfo();

				AlertDialog.Builder builder = new AlertDialog.Builder(
						SPen_Example_Simple.this);
				builder.setTitle("Color Selected");
				builder.setSingleChoiceItems(items, -1,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								switch (which) {
								case 0:
									// mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
									PP.setPenType(PP.getPenType());
									PP.setPenColor(Color.BLACK);
									// PP.setPenAlpha(5);
									mSCanvas.setPenSettingInfo(PP);
									char x = '#';
									char y = '4';
								
									//Log.d("TakeNote", "a : " + a);
									 UDPsendMessage(x, y);
									break;
								case 1:

									// PP.setPenAlpha(PP.getPenAlpha());
									PP.setPenType(PP.getPenType());
									PP.setPenColor(Color.RED);
									// PP.setPenAlpha(5);
									mSCanvas.setPenSettingInfo(PP);
									// mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
									// strokeInfo.setStrokeColor(Color.RED);
									// mSCanvas.setSettingStrokeInfo
									// (strokeInfo);
									char x1 = '#';
									char y1 = '5';
								
									//Log.d("TakeNote", "a : " + a);
									 UDPsendMessage(x1, y1);
									break;
								case 2:
									// mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
									PP.setPenType(PP.getPenType());
									PP.setPenColor(Color.BLUE);
									// PP.setPenAlpha(5);
									mSCanvas.setPenSettingInfo(PP);
									char x11 = '#';
									char y11 = '6';
								
									//Log.d("TakeNote", "a : " + a);
									 UDPsendMessage(x11, y11);
									break;

								}
							}
						});
				builder.setPositiveButton("확인",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				builder.show();

				// strokeInfo.setStrokeWidth(myStrokeWidth);

				// float x = mSCanvas.getX();
				// float y = mSCanvas.getY();
				// mSCanvas.panTo(x, y);

			}
			// mUndoBtn.setEnabled(mSCanvas.isUndoable());
			// mRedoBtn.setEnabled(mSCanvas.isRedoable());
		}
	};
	private OnClickListener redoBtnClickListener = new OnClickListener() {
		public void onClick(View v) {

			if (v.equals(mRedoBtn)) {

				final String items[] = { "Pen", "Marker" };
				final SettingStrokeInfo strokeInfo;
				final float Markerstyle = 5.0f;
				strokeInfo = mSCanvas.getSettingStrokeInfo();
				// final PenSettingInfo PP = new PenSettingInfo();

				AlertDialog.Builder builder = new AlertDialog.Builder(
						SPen_Example_Simple.this);
				builder.setTitle("Style Selected");
				builder.setSingleChoiceItems(items, -1,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								switch (which) {
								case 0:
									// mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
									PP.setPenType(0);
									PP.setPenColor(PP
											.getPenColor(PenSettingInfo.PEN_TYPE_MARKER));
									PP.setPenWidth(1);
									// strokeInfo.setStrokeStyle(PenSettingInfo.PEN_TYPE_MARKER);

									mSCanvas.setPenSettingInfo(PP);
									break;
								case 1:
									// mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);

									PP.setPenType(3);
									PP.setPenColor(PP
											.getPenColor(PenSettingInfo.PEN_TYPE_SOLID));
									// strokeInfo.setStrokeStyle(PenSettingInfo.PEN_TYPE_MARKER);

									mSCanvas.setPenSettingInfo(PP);
									break;

								}
							}
						});
				builder.setPositiveButton("확인",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				builder.show();

				// mSCanvas.redo();
			}

			// mUndoBtn.setEnabled(mSCanvas.isUndoable());
			// mRedoBtn.setEnabled(mSCanvas.isRedoable());
		}
	};
	OnClickListener mBtnClickListener = new OnClickListener() {

		public void onClick(View v) {
			int nBtnID = v.getId();
			// If the mode is not changed, open the setting view. If the mode is
			// same, close the setting view.
			if (nBtnID == mPenBtn.getId()) {
				if (mSCanvas.getCanvasMode() == SCanvasConstants.SCANVAS_MODE_INPUT_PEN) {
					mSCanvas.toggleShowSettingView(SCanvasConstants.SCANVAS_SETTINGVIEW_PEN);

				} else {
					mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
					mSCanvas.showSettingView(
							SCanvasConstants.SCANVAS_SETTINGVIEW_PEN, false);// pen �ㅼ젙李��꾩슦湲�for �뚯뒪��
					updateModeState();

				}
			} else if (nBtnID == mEraserBtn.getId()) {
				if (mSCanvas.getCanvasMode() == SCanvasConstants.SCANVAS_MODE_INPUT_ERASER) {
					mSCanvas.toggleShowSettingView(SCanvasConstants.SCANVAS_SETTINGVIEW_ERASER);
				} else {
					mSCanvas.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);
					mSCanvas.showSettingView(
							SCanvasConstants.SCANVAS_SETTINGVIEW_ERASER, false);
					updateModeState();
				}
			}
		}
	};

	private void updateModeState() {
		int nCurMode = mSCanvas.getCanvasMode();
		if (nCurMode == SCanvasConstants.SCANVAS_MODE_INPUT_PEN) {
			mPenBtn.setTextColor(Color.WHITE);
			mEraserBtn.setTextColor(Color.BLACK);
		} else if (nCurMode == SCanvasConstants.SCANVAS_MODE_INPUT_ERASER) {
			mPenBtn.setTextColor(Color.BLACK);
			mEraserBtn.setTextColor(Color.WHITE);
		}
	}

	// ------------------------------------------------
	// SettingView Listener
	// ------------------------------------------------
	SettingStrokeChangeListener mSettingStrokeChangeListener = new SettingStrokeChangeListener() {

		public void onClearAll(boolean bClearAllCompleted) {
			if (bClearAllCompleted)
				Log.i(TAG, "Clear All is completed");
		}

		public void onEraserWidthChanged(int eraserWidth) {
			Log.i(TAG, "Eraser width is changed : " + eraserWidth);
		}

		public void onStrokeColorChanged(int strokeColor) {
			Log.i(TAG, "Pen Color is changed : " + strokeColor);
		}

		public void onStrokeStyleChanged(int strokeStyle) {

			if (strokeStyle == SObjectStroke.SAMM_STROKE_STYLE_PENCIL)

				Log.i(TAG, "Stroke Style = Pen");
			// Toast.makeText(this,"dd",Toast.LENGTH_LONG).show();
			else if (strokeStyle == SObjectStroke.SAMM_STROKE_STYLE_BRUSH)
				Log.i(TAG, "Stroke Style = Brush");
			else if (strokeStyle == SObjectStroke.SAMM_STROKE_STYLE_CRAYON)
				Log.i(TAG, "Stroke Style = Pencil Crayon");
			else if (strokeStyle == SObjectStroke.SAMM_STROKE_STYLE_MARKER)
				Log.i(TAG, "Stroke Style = Marker");

		}

		public void onStrokeWidthChanged(int strokeWidth) {

		}

		public void onStrokeAlphaChanged(int strokeAlpha) {
			Log.i(TAG, "Pen alpha is changed : " + strokeAlpha);
		}

	};

	private HistoryUpdateListener mHistoryUpdateListener = new HistoryUpdateListener() {

		public void onHistoryChanged(boolean bUndoable, boolean bRedoable) {
			// float x;
			// float y;

			// x = mSCanvas.getX();
			// y = mSCanvas.getY();
			//
			// System.out.println(y);
			// System.out.println(x);
			// mUndoBtn.setEnabled(bUndoable);
			// mRedoBtn.setEnabled(bRedoable);

		}
	};

	SCanvasInitializeListener mSCanvasInitializeListener = new SCanvasInitializeListener() {
		public void onInitialized() {

			SettingStrokeInfo strokeInfo;
			strokeInfo = mSCanvas.getSettingStrokeInfo();
			PP.setPenColor(Color.BLACK);
			mSCanvas.setPenSettingInfo(PP);
			strokeInfo.setStrokeWidth(myStrokeWidth);
			mSCanvas.setSettingStrokeInfo(strokeInfo);

	
		}
	};
	//connectThread : 서버 접속 함수 , 메인쓰레드 에서 소켓생성 불가 쓰레드로 접속 가능
    class connectThread extends Thread {
	     private boolean flag = false; // 스레드 유지(종료)용 플래그
	  
	     public void run() {
	      try {
	    

	    		//  InetAddress serverAddr  = InetAddress.getByName("210.118.69.110");
	    	   Socket  cSocke = new Socket("210.118.69.110", 7800);
	    	  streamOut = new PrintWriter(cSocke.getOutputStream(), true);      // 출력용 스트림
	    	 streamIn = new BufferedReader(new InputStreamReader(cSocke.getInputStream()));  // 입력용 스트림
	    	  Log.d("device", "ddtr");
				
		
	    	  
	 
	    	  
	    	  
	    	  /*
	    	  BufferedReader in = new BufferedReader(new InputStreamReader(cSocke.getInputStream()));
	    			                   String str = in.readLine();
	    			                   Log.d("device", str);
	    			                   System.out.println("수신중인 파일 이름 : " + str);
	    			                   File f = new File("/sdcard/" +"1.jpg");
	    			                  //Log.d("device", "ggggg");
	    			                  FileOutputStream fout = new FileOutputStream(f);
	    			                 BufferedOutputStream  bout = new BufferedOutputStream(new DataOutputStream(fout));
	    			                 DataInputStream  bin = new DataInputStream(cSocke.getInputStream());
	    			                   DataOutputStream returnOut = new DataOutputStream(cSocke.getOutputStream());
	    			           
	    			                   byte headerBuffer[] = new byte[4];
	    			                   byte LengthBuffer[] = new byte[512];
	    			                   //read Header(header 에는 BodySize가 옴.)
	    			                      for(int i=0;i<4;i++){
	    			                    Log.i("headerBuffer confirm",""+headerBuffer[i]);
	    			                   }
	    			                   //Log.i("allBuffer Size",""+size);
	    			                      //long bodysize = hex.  headerBuffer); //16진수 bodysize를 10진수로 얻어옴
	    			                      //Log.i("bodysize : ",""+bodysize);
	    			                      
	    			                      long readsize=0;
	    		*/	                      
	    	   //sendMessage("새로운 이용자님이 들어왔습니다.");
	  
   	  
	       while (!flag) { // 플래그가 false일경우에 루프
	       
	           }               
	          
	       
	      }catch(Exception e) {
	   
	      }
	      	
	      
	     }     
	    }
    //UDP방식--c#의 ListenUDP방식으로 사용
    private void UDPsendMessage(float x, float y)  {
    		try{
      	
     		  
     		  
  				UDPsocket = new DatagramSocket();
  				
  				serverAddr = InetAddress.getByName("210.118.69.110");
  			  String msg = x + "/" + y ;
  			  byte[] buf = new byte[2];
  			
  				buf = msg.getBytes();
  			
  				DatagramPacket Packet = new DatagramPacket(buf, buf.length,
  						serverAddr, 7800);
  			
  				UDPsocket.send(Packet);
  		
    		}catch(IOException exx){
    			  Log.d("device", "여기까지올까sss");
    		}
    }
    //오버라이드 UDP send
  
    private void UDPsendMessage(char a, char b)  {
		try{
  	
 		  
 		  
				UDPsocket = new DatagramSocket();
				
				serverAddr = InetAddress.getByName("210.118.69.110");
			  String msg = a + "/" + b ;
			  byte[] buf = new byte[2];
			
				buf = msg.getBytes();
			
				DatagramPacket Packet = new DatagramPacket(buf, buf.length,
						serverAddr, 7800);
			
				UDPsocket.send(Packet);
				  Log.d("device", "부산스타일");
		}catch(IOException exx){
			  Log.d("device", "여기까지올까sss");
		}
}
   
    
    
    //---TCP비동기방식-------------C#의 Listen쓰레드 랑 같이 사용-----
    private void sendMessage(String MSG) {
	     try {
	    	// Thread.sleep(100);
	      streamOut.println(MSG);     // 서버에 메세지를 보내줍니다.
	     } catch (Exception ex) {
	  
	     }
	     
	     
    }
	

}
